package com.example.startscreen;

public class MyCityPolygon {
	public static double PI = 3.14159265;
	public static double TWOPI = 2 * PI;

	int cityID;
	String cityName;
	LatLongPair[] llArr;
	
	public MyCityPolygon (int cityID, String cityName, LatLongPair[] cityArr) {
		this.cityID = cityID;
		this.cityName = cityName;
		this.llArr = cityArr;
	}
	
	public static boolean coordinate_is_inside_polygon(double latitude, double longitude, LatLongPair[] llArr) {
		int i;
		double angle = 0;
		double point1_lat;
		double point1_long;
		double point2_lat;
		double point2_long;
		int n = llArr.length;

		for (i = 0; i < n; i++) {
			point1_lat = llArr[i].lat - latitude;
			point1_long = llArr[i].lon - longitude;
			point2_lat = llArr[(i + 1) % n].lat - latitude; // here be  dragons.
			point2_long = llArr[(i + 1) % n].lon - longitude;
			angle += Angle2D(point1_lat, point1_long, point2_lat, point2_long);
		}

		if (Math.abs(angle) < PI)
			return false;
		else
			return true;
	}

	public boolean coordinate_is_inside_polygon(double latitude, double longitude) {
		int i;
		double angle = 0;
		double point1_lat;
		double point1_long;
		double point2_lat;
		double point2_long;
		int n = llArr.length;

		for (i = 0; i < n; i++) {
			point1_lat = llArr[i].lat - latitude;
			point1_long = llArr[i].lon - longitude;
			point2_lat = llArr[(i + 1) % n].lat - latitude; // here be  dragons.
			point2_long = llArr[(i + 1) % n].lon - longitude;
			angle += Angle2D(point1_lat, point1_long, point2_lat, point2_long);
		}

		if (Math.abs(angle) < PI)
			return false;
		else
			return true;
	}

	public static double Angle2D(double x1, double y1, double x2, double y2) {
		double dtheta, theta1, theta2;

		theta1 = Math.atan2(y1, x1);
		theta2 = Math.atan2(y2, x2);
		dtheta = theta2 - theta1;
		while (dtheta > PI)
			dtheta -= TWOPI;
		while (dtheta < -PI)
			dtheta += TWOPI;

		return (dtheta);
	}

	public static boolean is_valid_gps_coordinate(double latitude, double longitude) {
		if (latitude > -90 && latitude < 90 && longitude > -180
				&& longitude < 180) {
			return true;
		}
		return false;
	}


}
